$(document).ready(function(){
	var currentPage = 0;
	var pageSize = 10;
	var token = $.zui.store.get("token");
	var userId = $.zui.store.get("userId");
	//用于分页查询
	var queryAreaListFun = function(){
		var url = adminQueryUrl + areaService;
		console.log('url2='+url+",toke="+token+",userId="+userId);
		var param = 'action=queryListPage';//请求到列表页面
		if ($("#areaNameLike").val()!=''){
			param += "&areaNameLike="+$("#areaNameLike").val()
		}
		if ($("#areaCode").val()!=''){
			param += "&areaCode="+$("#queryAreaCode").val()
		}
		if (currentPage>0){
			param += "&currentPage="+(currentPage-1);
		}
		param += "&pageSize="+pageSize;
		$.ajax({
			url:url,
			type:'post',
			dataType:'JSON',
			beforeSend:function(xhr){//设置请求头信息
				xhr.setRequestHeader("token:'"+token+"'");
				xhr.setRequestHeader("userId:'"+userId+"'");
			},
			headers:{'token':token,'userId':userId},
			data:param,
			success:function(data){
				console.log('data2='+data);
				$('#areaDataGrid').empty();
				$('#areaDataGrid').data('zui.datagrid',null);
				$('#areaPager').empty();
				$('#areaPager').data('zui.pager',null);
				if (data.success == '1'){
					if (data.pageTotal > 0){
						$('#areaDataGrid').datagrid({
							checkable:true,
					        checkByClickRow: true,
						    dataSource: {/*
						    	configs:{
						        	C1:{
						        		valueOperator: {
							                getter: function(dataValue, cell, dataGrid) {
							                	console.log("33="+dataValue);
							                    return '33';
							                }
								        }
						        	}
						        },*/
						        cols:[
						        	{name: 'areaId', label: '1', width: 1},
						            {name: 'areaCode', label: '地区编码', width: 132},
						            {name: 'areaName', label: '地区名称', width: 134},
						            {name: 'areaParentName', label: '上级地区名称', width: 109},
						            {name: 'sortNo', label: '排序码', width: 109},
						            {name: 'description', label: '描述', width: -1},//-1用于自适应宽度
						        ],
						        cache:false,
						        array:data.pageList
						    }
						});
						//获取分页
						$('#areaPager').pager({
						    page: data.currentPage,
						    recTotal: data.pageTotal,
						    recPerPage:pageSize,
						    elements:['first_icon', 'prev_icon', 'pages', 'next_icon', 'last_icon', 'total_text','size_menu','goto'],
						    pageSizeOptions:[10,20,30,50,100]
						});
					}
					else {
						$("#areaDataGrid").html("<center>查无数据</center>");
					}
				}
				else {
					$("#areaDataGrid").html("查无数据");
				}
			},
			error:function(e){
				new $.zui.Messager('系统繁忙,请稍候再试!', {
				    type: 'warning',
				    placement:'center'
				}).show();
			}
		});
	};
	queryAreaListFun();
	//监听分页，修改当前页，条数
	$('#areaPager').on('onPageChange', function(e, state, oldState) {
		currentPage = state.page;
		pageSize = state.recPerPage;
		console.log('currentPage='+currentPage+",pageSize="+pageSize);
		queryAreaListFun();
	});
	//搜索
	$("#searchBtn").click(function(){
		queryAreaListFun();
	});
	
	//初始化添加
	$("#addBtn").click(function(){
		$("#addForm")[0].reset();
		var url = adminQueryUrl + areaService;
		var param = 'action=queryAllList';//请求到列表页面
		$.ajax({
			url:url,
			type:'post',
			dataType:'JSON',
			beforeSend:function(xhr){//设置请求头信息
				xhr.setRequestHeader("token:'"+token+"'");
				xhr.setRequestHeader("userId:'"+userId+"'");
			},
			headers:{'token':token,'userId':userId},
			data:param,
			success:function(data){
				console.log('data='+data);
				if (data.success=='1'){
					$.each(data.areaList, function(i, item){     
						$("#areaParentCode").append("<option value='"+item.areaCode+"'>"+item.areaName+"</option>");
					});  
				}
			},
			error:function(e){
				new $.zui.Messager('系统繁忙,请稍候再试!', {
				    type: 'warning',
				    placement:'center'
				}).show();
			}
		});
	});
	//添加保存
	$("#addSave").click(function(){
		var validAreaCodeUrl = adminQueryUrl + areaService + "?action=validAreaCode";
		var validAreaNameUrl = adminQueryUrl + areaService + "?action=validAreaName";
		$("#addForm").validate({
			rules:{
				areaCode:{
					"required":true,
					"remote":{
						url:validAreaCodeUrl,
						type:'get',
						dataType:'json',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token:'"+token+"'");
							xhr.setRequestHeader("userId:'"+userId+"'");
						},
						headers:{'token':token,'userId':userId},
						data:{
							'areaCode':function(){
								return $("#areaCode").val();
							}
						}
					}
				},
				areaName:{
					"required":true,
					"remote":{
						url:validAreaNameUrl,
						type:'get',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token:'"+token+"'");
							xhr.setRequestHeader("userId:'"+userId+"'");
						},
						headers:{'token':token,'userId':userId},
						dataType:'json',
						data:{
							'areaName':function(){
								return $("#areaName").val();
							},
							'areaParentCode':function(){
								return $("#areaParentCode").val();
							}
						}
					}
				},
				sortNo:{
					"required":true,
					"digits":true
				}
			},
			messages:{
				areaCode:{
					"remote":"地区编码已经存在"
				},
				areaName:{
					"remote":"地区名称已经存在"
				}
			},
			submitHandler:function(form){
				$.ajax({
					url:adminQueryUrl + areaService,
					type:'post',
					dataType:'JSON',
					beforeSend:function(xhr){//设置请求头信息
						xhr.setRequestHeader("token:'"+token+"'");
						xhr.setRequestHeader("userId:'"+userId+"'");
					},
					headers:{'token':token,'userId':userId},
					data:$(form).serialize(),
					success:function(data){
						console.log('data='+data);
						if (data.success == '1') {
		                	new $.zui.Messager('添加成功!', {
		    				    type: 'success',
		    				    placement:'center'
		    				}).show();
		                	$('#addModal').modal('hide', 'fit');
		                	queryAreaListFun();
		                }
		                else{
		                	new $.zui.Messager(data.msg, {
		    				    type: 'warning',
		    				    placement:'center'
		    				}).show();
		                }
					},
					error:function(e){
						new $.zui.Messager('系统繁忙,请稍候再试!', {
	    				    type: 'warning',
	    				    placement:'center'
	    				}).show();
					}
				});
			}
		});
	});
	//初始化修改
	$("#updateBtn").click(function(){
		// 获取数据表格实例
		var areaDataGrid = $('#areaDataGrid').data('zui.datagrid');
		// 获取当前已选中的行数据项
		var selectedItems = areaDataGrid.getCheckItems();
		if (selectedItems.length == 0) {
			new $.zui.Messager('请选择要修改的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else if (selectedItems.length > 1) {
			new $.zui.Messager('请只选择一条要修改的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else {
			$("#updateForm")[0].reset();
			var url = adminQueryUrl + areaService;
			var param = 'action=initUpdate&areaId='+selectedItems[0].areaId;//请求到列表页面
			console.log('param='+param);
			$.ajax({
				url:url,
				type:'post',
				dataType:'JSON',
				beforeSend:function(xhr){//设置请求头信息
					xhr.setRequestHeader("token:'"+token+"'");
					xhr.setRequestHeader("userId:'"+userId+"'");
				},
				headers:{'token':token,'userId':userId},
				data:param,
				success:function(data){
					console.log('data='+data);
					if (data.success=='1'){
						$('#updateModal').modal('show', 'fit');
						$.each(data.areaList, function(i, item){   
							if (data.areaMap.areaParentCode == item.areaCode){
								$("#updateAreaParentCode").append("<option value='"+item.areaCode+"' selected='selected'>"+item.areaName+"</option>");
							}
							else {
								$("#updateAreaParentCode").append("<option value='"+item.areaCode+"'>"+item.areaName+"</option>");
							}
						});  
						$("#updateAreaCode").val(data.areaMap.areaCode);
						$("#updateAreaName").val(data.areaMap.areaName);
						$("#updateSortNo").val(data.areaMap.sortNo);
						$("#updateDescription").val(data.areaMap.description);
						$("#updateAreaId").val(data.areaMap.areaId);
					}
				},
				error:function(e){
					new $.zui.Messager('系统繁忙,请稍候再试!', {
					    type: 'warning',
					    placement:'center'
					}).show();
				}
			});
		}
	});
	//修改保存
	$("#updateSave").click(function(){
		var validAreaCodeUrl = adminQueryUrl + areaService + "?action=validAreaCode";
		var validAreaNameUrl = adminQueryUrl + areaService + "?action=validAreaName";
		$("#updateForm").validate({
			rules:{
				updateAreaCode:{
					"required":true,
					"remote":{
						url:validAreaCodeUrl,
						type:'get',
						dataType:'json',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token:'"+token+"'");
							xhr.setRequestHeader("userId:'"+userId+"'");
						},
						headers:{'token':token,'userId':userId},
						data:{
							'areaCode':function(){
								return $("#updateAreaCode").val();
							},
							'excludeAreaId':function(){
								return $("#updateAreaId").val();
							}
						}
					}
				},
				updateAreaName:{
					"required":true,
					"remote":{
						url:validAreaNameUrl,
						type:'get',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token:'"+token+"'");
							xhr.setRequestHeader("userId:'"+userId+"'");
						},
						headers:{'token':token,'userId':userId},
						dataType:'json',
						data:{
							'areaName':function(){
								return $("#updateAreaName").val();
							},
							'areaParentCode':function(){
								return $("#updateAreaParentCode").val();
							},
							'excludeAreaId':function(){
								return $("#updateAreaId").val();
							}
						}
					}
				},
				updateSortNo:{
					"required":true,
					"digits":true
				}
			},
			messages:{
				updateAreaCode:{
					"remote":"地区编码已经存在"
				},
				updateAreaName:{
					"remote":"地区名称已经存在"
				}
			},
			submitHandler:function(form){
				$.ajax({
					url:adminQueryUrl + areaService+"?action=update",
					type:'post',
					dataType:'JSON',
					beforeSend:function(xhr){//设置请求头信息
						xhr.setRequestHeader("token:'"+token+"'");
						xhr.setRequestHeader("userId:'"+userId+"'");
					},
					headers:{'token':token,'userId':userId},
					data:$(form).serialize(),
					success:function(data){
						console.log('data='+data);
						if (data.success == '1') {
		                	new $.zui.Messager('修改成功!', {
		    				    type: 'success',
		    				    placement:'center'
		    				}).show();
		                	$('#updateModal').modal('hide', 'fit');
		                	queryAreaListFun();
		                }
		                else{
		                	new $.zui.Messager(data.msg, {
		    				    type: 'warning',
		    				    placement:'center'
		    				}).show();
		                }
					},
					error:function(e){
						new $.zui.Messager('系统繁忙,请稍候再试!', {
	    				    type: 'warning',
	    				    placement:'center'
	    				}).show();
					}
				});
			}
		});
	});
	//显示删除确认框
	$("#deleteBtn").click(function(){
		// 获取数据表格实例
		var areaDataGrid = $('#areaDataGrid').data('zui.datagrid');
		// 获取当前已选中的行数据项
		var selectedItems = areaDataGrid.getCheckItems();
		if (selectedItems.length == 0) {
			new $.zui.Messager('请选择要删除的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else if (selectedItems.length > 1) {
			new $.zui.Messager('请只选择一条要删除的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else {
			$('#deleteModal').modal('show', 'fit');
		}
	});
	//删除地区
	$("#deleteAreaBtn").click(function(){
		// 获取数据表格实例
		var areaDataGrid = $('#areaDataGrid').data('zui.datagrid');
		// 获取当前已选中的行数据项
		var selectedItems = areaDataGrid.getCheckItems();
		var url = adminQueryUrl + areaService;
		var param = 'action=delete&areaCode='+selectedItems[0].areaCode;//请求到列表页面
		console.log('param2='+param);
		$.ajax({
			url:url,
			type:'post',
			dataType:'JSON',
			beforeSend:function(xhr){//设置请求头信息
				xhr.setRequestHeader("token:'"+token+"'");
				xhr.setRequestHeader("userId:'"+userId+"'");
			},
			headers:{'token':token,'userId':userId},
			data:param,
			success:function(data){
				console.log('data='+data);
				$('#deleteModal').modal('hide', 'fit');
				if (data.success=='1'){
	                	new $.zui.Messager('删除成功!', {
	    				    type: 'success',
	    				    placement:'center'
	    				}).show();
	                	queryAreaListFun();
				}
                else{
                	new $.zui.Messager(data.msg, {
    				    type: 'warning',
    				    placement:'center'
    				}).show();
				}
			},
			error:function(e){
				new $.zui.Messager('系统繁忙,请稍候再试!', {
				    type: 'warning',
				    placement:'center'
				}).show();
			}
		});
	});
	//初始化详情
	$("#detailBtn").click(function(){
		// 获取数据表格实例
		var areaDataGrid = $('#areaDataGrid').data('zui.datagrid');
		// 获取当前已选中的行数据项
		var selectedItems = areaDataGrid.getCheckItems();
		if (selectedItems.length == 0) {
			new $.zui.Messager('请选择要查看的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else if (selectedItems.length > 1) {
			new $.zui.Messager('请只选择一条要查看的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else {
			$("#detailForm")[0].reset();
			var url = adminQueryUrl + areaService;
			var param = 'action=detail&areaId='+selectedItems[0].areaId;//请求到列表页面
			console.log('param='+param);
			$.ajax({
				url:url,
				type:'post',
				dataType:'JSON',
				beforeSend:function(xhr){//设置请求头信息
					xhr.setRequestHeader("token:'"+token+"'");
					xhr.setRequestHeader("userId:'"+userId+"'");
				},
				headers:{'token':token,'userId':userId},
				data:param,
				success:function(data){
					console.log('data='+data);
					if (data.success=='1'){
						$('#detailModal').modal('show', 'fit');
						$("#detailAreaCode").html(data.areaMap.areaCode);
						$("#detailAreaName").html(data.areaMap.areaName);
						$("#detailAreaParentCode").html(data.areaMap.areaParentName);
						$("#detailSortNo").html(data.areaMap.sortNo);
						$("#detailOprId").html(data.areaMap.trueName);
						$("#detailOprDate").html(data.areaMap.oprDate);
						$("#detailDescription").html(data.areaMap.description);
					}
				},
				error:function(e){
					new $.zui.Messager('系统繁忙,请稍候再试!', {
					    type: 'warning',
					    placement:'center'
					}).show();
				}
			});
		}
	});
});

